<?php
	class Question extends MY_controller{

		function __construct(){
			parent::__construct();
			$this->smarty->assign("menu_id", 7);
		}

		function index($category_id = 0, $type = 1, $limit = 0){
			$post_data = $this->input->post();
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('category_id' => $category_id, "school_id" => $this->school_id, "type" => $type);
			$this->load->model("Questions");
			$result = $this->Questions->get_question_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = "/question/index/$category_id/$type";
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 4;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$hot_question = $this->get_hot_question($category_id, 10);
			
			
			
			$this->smarty->assign("action", '分类列表');
			$this->smarty->assign("type", $type);
			$this->smarty->assign("category_id", $category_id);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("question_list", $result['result']);
			$this->smarty->assign("hot_question", $hot_question['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("question/list.html");
		}

		function create(){
			$post_data = $this->input->post();
			if(!empty($post_data)){
				if(empty($this->user_data['id'])){
					show_error_message("请先登录", "/question");exit;
				}
				if(!empty($post_data['category_id']) && !empty($post_data['category_name']) && !empty($post_data['title'])){
					$question_data['category_id'] 	= $post_data['category_id'];
					$question_data['category_name'] = $post_data['category_name'];
					$question_data['title'] 		= trim($post_data['title']);
					$question_data['uid'] 			= $this->user_data['id'];
					$question_data['content'] 		= trim($post_data['content']);
					$question_data['tid'] 			= !empty($post_data['tid']) ? $post_data['tid'] : 0;
					$question_data['tname'] 		= !empty($post_data['tname']) ? $post_data['tname'] : '';
					$question_data['school_id'] 	= $this->school_id;
					$question_data['add_time']		= time();
					$question_data['anonymous']		= !empty($post_data['anonymous']) ? $post_data['anonymous'] : 1;
					$question_data['status']		= !empty($post_data['status']) ? $post_data['status'] : 1;
					$this->db->insert("questions", $question_data);
					if($this->db->insert_id() > 0 && $question_data['tid'] > 0){
						$this->load->model("Question_topics");
						$this->Question_topics->add_question_num($question_data['tid']);
						$this->smarty->assign("status", "success");
						$this->smarty->display("question/create.html");
					}
				}
			}else{
				$this->load->model("Question_categorys");
				$this->smarty->assign("status", 1);
				$category_list = $this->Question_categorys->get_top_categorys();
				$this->smarty->assign("category_list", $category_list);
				$this->smarty->display("question/create.html");
			}
		}

		function search_topic(){
			$post_data = $this->input->post();
			if(!empty($post_data['topic_name'])){
				$this->load->model("Question_topics");
				$conditions['school_id'] = $this->school_id;
				$conditions['name'] = trim($post_data['topic_name']);
				$topic_list = $this->Question_topics->get_question_topic_list(0, 10, $conditions);
				if($topic_list['count'] > 0){
					$result['result_code'] = 1;
					$result['result_data'] = $topic_list['result'];
				}else{
					$result['result_code'] = 0;
				}
			}else{
				$result['result_code'] = 2;
			}
			echo json_encode($result);
		}

		function add_topic(){
			if(!is_ajax_request()){
				redirect("/question/index");exit;
			}
			$post_data = $this->input->post();
			if(!$this->user_data['id']){
				$result['result_code'] = 9;
			}else{
				if(!empty($post_data['topic_name'])){
					$topic_name = trim($post_data['topic_name']);
					$question_topic = get_field_by_where("question_topics", "id, name", "name='{$topic_name}'");
					if(!empty($question_topic)){
						$result['result_code'] = 1;
						$result['result_data'] = $question_topic;
					}else{
						$topic_data['uid'] 			= $this->user_data['id'];
						$topic_data['name'] 		= $topic_name;
						$topic_data['school_id'] 	= $this->school_id;
						$topic_data['add_time'] 	= time();
						$this->db->insert("question_topics", $topic_data);
						$result['result_code'] = 1;
						$result['result_data'] = array("id" => $this->db->insert_id(), "name" => $topic_name);
					}
				}else{
					$result['result_code'] = 2;
				}
			}
			echo json_encode($result);
		}

		function detail($id = 0, $limit = 0){
			$question_info = get_field_by_where("questions", "*", "id='{$id}'");
			if($question_info){
				$this->load->model("Questions");
				$this->Questions->increase_click_num($id);
				$conditions['qid'] = $id;
				$answer_list = $this->Questions->get_question_answer($limit, $this->offset = 10, $conditions);
				$this->load->library('pagination');
				$page_config['base_url'] = "/question/detail/$id";
				$page_config['total_rows'] = $answer_list['count'];
				$page_config['per_page'] = $this->offset;
				$page_config['uri_segment'] = 4;
				$this->pagination->initialize($page_config);
				$pagination = $this->pagination->create_links();

				$hot_question = $this->get_hot_question(0, 10);
				$this->smarty->assign("hot_question", $hot_question['result']);

				$this->smarty->assign("question_info", $question_info);
				$this->smarty->assign("pagination", $pagination);
				$this->smarty->assign("answer_list", $answer_list['result']);
				$this->smarty->assign("uid", $this->user_data['id']);
				$this->smarty->display("question/detail.html");
			}else{
				show_message($_SERVER['HTTP_REFERER'], "您查看的提问不存在");
			}
		}

		function add_answer(){
			if(!$this->user_data['id']){
				//没登录
				redirect("/login");exit;
			}else{
				$post_data = $this->input->post();
				if($this->user_data['id'] <= 0){
					show_error_message($_SERVER['HTTP_REFERER'], "请先登录");exit;
				}

				if(empty($post_data['qid'])){
					show_error_message($_SERVER['HTTP_REFERER'], "缺少提问ID");exit;
				}
				
				if(empty($post_data['content'])){
					show_error_message($_SERVER['HTTP_REFERER'], "回复内容为空");exit;
				}

				$answer_data['uid'] = $this->user_data['id'];
				$answer_data['qid'] = $post_data['qid'];
				$answer_data['content'] = $post_data['content'];
				$answer_data['anonymous'] = !empty($post_data['anonymous']) ? $post_data['anonymous'] : 1;
				$answer_data['add_time'] = time();
				$this->db->insert("question_answers", $answer_data);
				if($this->db->insert_id() > 0){
					$this->load->model("Questions");
					$this->Questions->increase_answer_num($answer_data['qid']);
				}
				show_message($_SERVER['HTTP_REFERER'], "回复成功。");
			}
		}

		function my($category_id = 0, $type = 1, $limit = 0){
			if(!$this->user_data['id']){
				//没登录
				redirect("/login");exit;
			}else{
				$post_data = $this->input->post();
				$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
				$conditions = array('category_id' => $category_id, "school_id" => $this->school_id, "type" => $type, "uid" => $this->user_data['id']);
				$this->load->model("Questions");
				$result = $this->Questions->get_question_list($limit, $this->offset, $conditions);
				$this->load->library('pagination');
				$page_config['base_url'] = "/question/index/$category_id/$type";
				$page_config['total_rows'] = $result['count'];
				$page_config['per_page'] = $this->offset;
				$page_config['uri_segment'] = 4;
				$this->pagination->initialize($page_config);
				$pagination = $this->pagination->create_links();

				$hot_question = $this->get_hot_question($category_id, 10);
				$this->smarty->assign("hot_question", $hot_question['result']);

				$this->load->model("Question_categorys");
				$conditions['school_id'] = $this->school_id;
				$category_list = $this->Question_categorys->get_top_categorys();
				$this->smarty->assign("action", '分类列表');
				$this->smarty->assign("category_list", $category_list);
				$this->smarty->assign("type", $type);
				$this->smarty->assign("category_id", $category_id);
				$this->smarty->assign("current_url", $current_url);
				$this->smarty->assign("question_list", $result['result']);
				$this->smarty->assign("pagination", $pagination);
				$this->smarty->display("question/my_list.html");
			}
		}

		function my_answer($category_id = 0, $type = 1, $limit = 0){
			if(!$this->user_data['id']){
				//没登录
				redirect("/login");exit;
			}else{
				$post_data = $this->input->post();
				$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
				$conditions = array('category_id' => $category_id, "school_id" => $this->school_id, "type" => $type, "answer_uid" => $this->user_data['id']);
				$this->load->model("Questions");
				$result = $this->Questions->get_question_list($limit, $this->offset, $conditions);
				$this->load->library('pagination');
				$page_config['base_url'] = "/question/index/$category_id/$type";
				$page_config['total_rows'] = $result['count'];
				$page_config['per_page'] = $this->offset;
				$page_config['uri_segment'] = 4;
				$this->pagination->initialize($page_config);
				$pagination = $this->pagination->create_links();

				$hot_question = $this->get_hot_question($category_id, 10);
				$this->smarty->assign("hot_question", $hot_question['result']);

				$this->load->model("Question_categorys");
				$conditions['school_id'] = $this->school_id;
				$category_list = $this->Question_categorys->get_top_categorys();
				$this->smarty->assign("action", '分类列表');
				$this->smarty->assign("category_list", $category_list);
				$this->smarty->assign("type", $type);
				$this->smarty->assign("category_id", $category_id);
				$this->smarty->assign("current_url", $current_url);
				$this->smarty->assign("question_list", $result['result']);
				$this->smarty->assign("pagination", $pagination);
				$this->smarty->display("question/my_answer.html");
			}
		}

		function get_hot_question($category_id = 0, $offset = 2){
			$conditions = array('category_id' => 0, "school_id" => $this->school_id, "type" => 2);
			$this->load->model("Questions");
			$result = $this->Questions->get_question_list(0, $offset, $conditions);
			return $result;
		}
	}